﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BlueLotus7.Extensions
{
    public static class ViewUtilityExtension
    {
        const string StringEmpty = "";

        /// <summary>
        /// 
        /// </summary>
        public class WebAreaResource
        {
            public string CssFileName
            {
                get;
                set;
            }

            public string JavaScriptFileName
            {
                get;
                set;
            }

            public string AjaxUrlCollectionActionUrl
            {
                get;
                set;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="viewContext"></param>
        /// <param name="prefix"></param>
        /// <param name="surfix"></param>
        /// <param name="ajaxActionName"></param>
        /// <returns></returns>
        public static WebAreaResource GetAreaWebResource(ViewContext viewContext, string prefix = StringEmpty, string surfix = StringEmpty, string ajaxActionName = "RetrieveAjaxCollection")
        {
            WebAreaResource result = new WebAreaResource();

            object tempValue = viewContext.RequestContext.RouteData.DataTokens["area"];
            string currentArea = string.Empty;
            if (tempValue != null)
            {
                currentArea = tempValue.ToString();
            }
            string currentControllerName = viewContext.RequestContext.RouteData.Values["controller"].ToString();
            if (!string.IsNullOrWhiteSpace(currentArea))
            {
                currentControllerName = currentArea + "." + currentControllerName;
            }
            string resourceName = prefix + currentControllerName + surfix;
            result.JavaScriptFileName = resourceName + ".js";
            result.CssFileName = resourceName + ".css";

            UrlHelper urlHelper = new UrlHelper(viewContext.RequestContext);
            result.AjaxUrlCollectionActionUrl = urlHelper.Action(ajaxActionName, currentControllerName, new { area = currentArea });

            return result;
        }
    }
}